package com.sunlg.smarthome.contract.presenter;

import com.sunlg.smarthome.base.App;
import com.sunlg.smarthome.contract.LogoutContract;
import com.sunlg.smarthome.data.remote.MySubscriber;
import com.sunlg.smarthome.data.remote.Result;
import com.sunlg.smarthome.data.remote.SHServiceWrapper;
import com.sunlg.smarthome.utils.PreferHelper;

/**
 * Created by sunlg on 2017/4/21.
 */

public class LogoutPresenter implements LogoutContract.Presenter {

    private PreferHelper gatewayPreferHelper;
    private PreferHelper userPreferHelper;
    private SHServiceWrapper mSHServiceWrapper;

    public LogoutPresenter() {
        gatewayPreferHelper = new PreferHelper(App.getInstance(), "gateway");
        userPreferHelper = App.getInstance().getPreferHelper();
        mSHServiceWrapper = App.getInstance().getServiceWrapper();
    }

    @Override
    public void logout() {
        mSHServiceWrapper.logout(new MySubscriber<Result>() {
            @Override
            public void onSuccess(Result data) {
                if (data.getCode() == 0) {
                    gatewayPreferHelper.setInt("id", 0);
                    userPreferHelper.clear();
                } else {
                    _onError(new Throwable(data.getMsg()));
                }
            }

            @Override
            public void _onError(Throwable e) {
            }
        });
    }

}
